﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Fc
{
    public partial class 接箍入库单录入 : System.Web.UI.Page
    {
        private int billid = 0;
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if ((object)Session["OperatorDID"] == null)
                {
                    Session["OperatorDID"] = 0;
                }
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out billid);
                }
                if (billid > 0)
                {
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = "select 产品类别ID,合格,备注,判定员 from 自产接箍判定单 where ID=" + billid;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.LeiBie.Text = ((int)Reader[0]).ToString();
                        if (!Reader.IsDBNull(2))
                        {
                            this.BeiZhu.Value = (string)Reader[2];
                        }
                        if (!Reader.IsDBNull(3))
                        {
                            this.Label_ZhiJianYuan.Text = (string)Reader[3];
                        }
                        if ((bool)Reader[1])
                        {
                            this.Button_Save.Visible = false;
                            this.PDZ.Visible = true;
                            this.LeiBie.Enabled = false;
                            this.Button_PanDin.Text = "取消盖章";
                        }
                        this.Label_ID.Text = billid.ToString();
                        int did = (int)Session["OperatorDID"];
                        if (did == 0)
                        {
                            this.Button_PanDin.Visible = true;
                        }
                        Reader.Close();
                        Cmd.CommandText = "select 日期,原料供货ID,炉号,支数,紧密距 from 自产接箍明细 where 单据ID=" + billid;
                        Reader = Cmd.ExecuteReader();
                        while (Reader.Read())
                        {
                            this.RQList.Value += "|" + ((DateTime)Reader[0]).ToLongDateString();
                            this.GHIDList.Value += "|" + (int)Reader[1];
                            this.LHList.Value += "|" + (string)Reader[2];
                            this.ZSList.Value += "|" + (int)Reader[3];
                            this.JMList.Value += "|" + (string)Reader[4];
                        }
                        if (this.RQList.Value.Length > 0) this.RQList.Value = this.RQList.Value.Substring(1);
                        if (this.GHIDList.Value.Length > 0) this.GHIDList.Value = this.GHIDList.Value.Substring(1);
                        if (this.LHList.Value.Length > 0) this.LHList.Value = this.LHList.Value.Substring(1);
                        if (this.ZSList.Value.Length > 0) this.ZSList.Value = this.ZSList.Value.Substring(1);
                        if (this.JMList.Value.Length > 0) this.JMList.Value = this.JMList.Value.Substring(1);
                    }
                    else
                    {
                        this.Notice = "·出库单读取失败";
                    }
                    Reader.Close();

                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string[] rqli = this.RQList.Value.Split(new char[] { '|' });//日期列
            string[] ghli = this.GHIDList.Value.Split(new char[] { '|' });//供货商列
            string[] lhli = this.LHList.Value.Split(new char[] { '|' });//炉号列
            string[] zsli = this.ZSList.Value.Split(new char[] { '|' });//支数列
            string[] jmli = this.JMList.Value.Split(new char[] { '|' });//紧密距列

            DateTime d = DateTime.Today;
            int zhi = 0, zzhi = 0, ghid = 0;
            int n = 0;

            //录入检测开始
            for (int i = 0; i < rqli.Length; i++)
            {
                if (rqli[i] == "" && ghli[i] == "" && lhli[i] == "" && zsli[i] == "" && jmli[i] == "")
                {
                    continue;
                }

                if (rqli[i] == "" && i > 0)
                {
                    rqli[i] = d.ToLongDateString();
                }
                if (i > 0)
                {
                    if (ghli[i] == "")
                        ghli[i] = ghli[i - 1];
                    if (lhli[i] == "")
                        lhli[i] = lhli[i - 1];
                }
                if (jmli[i] == "")
                {
                    if (this.LeiBie.Text == "1" || this.LeiBie.Text == "3")
                    {
                        jmli[i] = "5-6.5";
                    }
                    else
                    {
                        jmli[i] = "7.5-9";
                    }
                }
                if (rqli[i] == "" || ghli[i] == "" || lhli[i] == "" || zsli[i] == "" || jmli[i] == "")
                {
                    this.Notice = "·保存失败，第" + (i + 1) + "行数据录入不全！";
                    return;
                }
                else
                {
                    if (!DateTime.TryParse(rqli[i], out d))
                    {
                        this.Notice = "·保存失败，第" + (i + 1) + "行日期录入有误！";
                        return;
                    }
                    else if (!int.TryParse(ghli[i], out ghid))
                    {
                        this.Notice = "·保存失败，检测到无效数据！";
                        return;
                    }
                    else if (!int.TryParse(zsli[i], out zhi))
                    {
                        this.Notice = "·保存失败，第" + (i + 1) + "行支数录入有误！";
                        return;
                    }
                    zzhi += zhi;
                    rqli[i] = d.ToShortDateString();
                }
                n++;
            }
            if (n == 0)
            {
                this.Notice = "·保存失败，至少要录入1行数据！";
                return;
            }
            //录入检测完成

            int.TryParse(this.Label_ID.Text, out billid);
            int leibie;
            double zz;
            this.BeiZhu.Value = this.BeiZhu.Value.Replace("'", "");

            leibie = int.Parse(this.LeiBie.Text);
            if (leibie == 1 || leibie == 3)
            {
                zz = 2.3;
            }
            else
            {
                zz = 6.1;
            }
            if (billid > 0)
            {
                this.SqlDataSource1.UpdateCommand = "update 自产接箍判定单 set 产品类别ID="
                    + leibie + ",日期='" + d.ToShortDateString() + "',支数合计="
                    + zzhi + ",重量合计=" + (zzhi * zz) + ",备注='" + this.BeiZhu.Value + "' where ID=" + billid
                    + ";delete from 自产接箍明细 where 单据ID=" + billid;
                this.SqlDataSource1.Update();
            }
            else
            {
                string op = (string)this.Session["OperatorName"];
                if (op == null)
                {
                    op = "";
                }

                this.SqlDataSource1.InsertCommand = "insert into 自产接箍判定单(产品类别ID,日期,支数合计,重量合计,备注,录入)values("
                    + leibie + ",'" + d.ToShortDateString() + "'," + zzhi + "," + (zzhi * zz) + ",'" + this.BeiZhu.Value + "','"+op+"');set @BillID=@@IDENTITY;";
                this.SqlDataSource1.Insert();
                this.Label_ID.Text = billid.ToString();
            }
            for (int i = 0; i < rqli.Length; i++)
            {
                if (rqli[i] == "" && ghli[i] == "" && lhli[i] == "" && zsli[i] == "" && jmli[i] == "")
                {
                    continue;
                }
                this.SqlDataSource1.UpdateCommand = "insert into 自产接箍明细(单据ID,类别ID,原料供货ID,日期,炉号,支数,重量,紧密距)values("
                    + billid + "," + leibie + "," + ghli[i] + ",'" + rqli[i] + "','"
                    + lhli[i].Replace("'", "") + "'," + zsli[i] + "," + (int.Parse(zsli[i]) * zz) + ",'" + jmli[i].Replace("'", "") + "')";
                this.SqlDataSource1.Update();
            }
            int did = (int)Session["OperatorDID"];
            if (did == 0)
            {
                this.Button_PanDin.Visible = true;
            }
            this.Notice = "·保存成功！";
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            billid = (int)e.Command.Parameters[0].Value;
        }

        protected void Button_PanDin_Click(object sender, EventArgs e)
        {
            if (this.Button_PanDin.Text == "盖 章")
            {
                string op = (string)this.Session["OperatorName"];
                if (op == null)
                {
                    op = "测试";
                }
                this.Button_PanDin.Text = "取消盖章";
                this.SqlDataSource1.UpdateCommand = "update 自产接箍判定单 set 合格=1,判定员='" + op.Replace("'", "") + "' where ID=" + this.Label_ID.Text;
                this.PDZ.Visible = true;
                this.Button_Save.Visible = false;
                this.LeiBie.Enabled = false;
            }
            else
            {
                this.Button_PanDin.Text = "盖 章";
                this.SqlDataSource1.UpdateCommand = "update 自产接箍判定单 set 合格=0,判定员='' where ID=" + this.Label_ID.Text;
                this.PDZ.Visible = false;
                this.Button_Save.Visible = true;
                this.LeiBie.Enabled = true;
            }
            this.SqlDataSource1.Update();
        }

    }
}
